<template>
	<xn-form-container
		:title="formData.id ? '编辑日程管理' : '增加日程管理'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="日程类型：" name="calendarType">
				<a-radio-group v-model:value="formData.calendarType" name="radioGroup">
					<a-radio value="0">私密</a-radio>
					<a-radio value="1">公开</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="日期：" name="date">
				<a-range-picker
					v-model:value="formData.date"
					show-time
					value-format="YYYY-MM-DD HH:mm:ss"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="摘要：" name="abstract">
				<a-input v-model:value="formData.abstract" placeholder="请输入摘要" allow-clear />
			</a-form-item>
			<a-form-item label="地点：" name="locations">
				<a-input v-model:value="formData.locations" placeholder="请输入地点" allow-clear />
			</a-form-item>
			<a-form-item label="具体事宜：" name="content">
				<a-input v-model:value="formData.content" placeholder="请输入具体事宜" allow-clear />
			</a-form-item>
			<a-form-item label="所属用户：" name="userId">
				<a-select ref="select" v-model:value="formData.userId" @focus="focus" @change="handleChange">
					<a-select-option :value="item.id" v-for="item in leaderList" :key="item.id">{{ item.name }}</a-select-option>
				</a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizCalendarManagementForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizLeaderCalendarApi from '@/api/biz/bizLeaderCalendarApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const leaderList = ref([])
	onMounted(() => {
		bizLeaderCalendarApi.bizLeaderList({ id: '' }).then((res) => {
			console.log(res)
			leaderList.value = res
		})
	})
	const focus = () => {
		console.log('focus')
	}

	const handleChange = (value) => {
		console.log(`selected ${value}`)
	}
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			record.date = [record.startDate, record.endDate]
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		calendarType: [required('请输入日程类型')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			console.log(formData.value, 789)

			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			console.log(formDataParam)
			formDataParam.startDate = formDataParam.date[0]
			formDataParam.endDate = formDataParam.date[1]
			bizLeaderCalendarApi
				.bizLeaderCalendarSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
